/**
 * Created by LeungZ on 2016/9/7.
 */
import jsYaml from 'js-yaml';
class Ctrl {
    constructor($modalInstance, appSrv, ctgNotification, service) {
        "ngInject";
        this._modalInstance = $modalInstance;
        this._notification = ctgNotification;
        this._appSrv = appSrv;

        // console.log(service)
        if(service){
            this._data = service;
            this.podPort = this._appSrv._dfPort;
            this.podDomain = this._data.url;
            if(this._data.isAutoScalable){
                this.maxPodNum = this._data.maxPodNum;
                this.minPodNum = this._data.minPodNum;
                this.atsTarget = this._data.elasticIndicator;
                this.atsStartTime = this._data.elasticStartTime && new Date(this._data.elasticStartTime).getTime();
                this.atsEndTime = this._data.elasticEndTime && new Date(this._data.elasticEndTime).getTime();
                this.atsSpace = this._data.timeRate;
                this.atsLimit = this._data.maxCpu;
                this.atsCheck = !!this._data.isAutoScalable;
            }
        }
    }

    save() {
        if(this.currentForm == 'domain'){
            this.$forms.domain.$submitted = true;
            if(this.$forms.domain.$invalid) return
        }
        if(this.currentForm == 'ats'){
            this.$forms.ats.$submitted = true;
            if(this.$forms.ats.$invalid) return
        }

        this.loading = true;
        this._appSrv.advUpdate(this, this._data.serviceId).then(()=> {
            this._notification.success('更新成功.');
            if(this.currentForm == 'domain'){
                this._data.url = this.podDomain;
                this._data.externalPort = this.podPort;
            }
            if(this.currentForm == 'ats'){
                this._data.isAutoScalable = this.atsCheck ? 1:0;
                if(this.atsCheck){
                    this._data.maxPodNum = this.maxPodNum;
                    this._data.minPodNum = this.minPodNum;
                    this._data.elasticIndicator = this.atsTarget;
                    this._data.elasticStartTime = +this.atsStartTime;
                    this._data.elasticEndTime = +this.atsEndTime;
                    this._data.timeRate = this.atsSpace;
                    this._data.maxCpu = this.atsLimit;
                }else{
                    this._data.maxPodNum = null;
                    this._data.minPodNum = null;
                    this._data.elasticIndicator = null;
                    this._data.elasticStartTime = null;
                    this._data.elasticEndTime = null;
                    this._data.atsSpace = null;
                    this._data.maxCpu = null;
                }
            }
        }).catch(data=> {
            this._notification.error('更新失败, ' + data.resultMsg);
        }).finally(()=>{
            this.loading = false;
        });
    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }

}
export default Ctrl